function countdown(minutes) {

	if (window.sessionStorage.currentDate == null) {
		// today's date
		var currentDate = new Date();
		window.sessionStorage.setItem("currentDate", currentDate.toDateString());
	} else {
		var currentDate = new Date(window.sessionStorage.currentDate);
	}
	if (window.sessionStorage.lastCountdownValue == null) {
		var seconds = minutes * 60;
		window.sessionStorage.setItem("lastCountdownValue", seconds);
	} else {
		var seconds = window.sessionStorage.lastCountdownValue;
	}
	
	if (seconds > 0) {
		seconds--;
		// show total time
		showInitial(currentDate, seconds);
		// start count down loop
		repeatCountdown(currentDate, seconds);
	} else {
		showInitial(currentDate, seconds);
	}
}
  
function repeatCountdown(currentDate, targetSeconds) {
	setTimeout(function() { 
		if (targetSeconds-- > 0) {
			window.sessionStorage.setItem("lastCountdownValue", targetSeconds);
			var targetDate = new Date(currentDate); 
			targetDate.setSeconds(currentDate.getSeconds() + targetSeconds);
			var msec = targetDate.getTime() - currentDate.getTime();
			var sec = Math.floor(msec / 1000);
			var mins = Math.floor(sec / 60);
			var hours = Math.floor(mins / 60);
			sec = sec % 60;
			mins = mins % 60;
			hours = hours % 24;

			// add leading zeros
			if (sec < 10) {
				sec = "0" + sec;
			}
			if (mins < 10) {
				mins = "0" + mins;
			}
			if (hours < 10) {
				hours = "0" + hours;
			}
			document.getElementById("countdown").innerHTML = hours + ":" + mins + ":" + sec;
			repeatCountdown(currentDate, targetSeconds);
		} else {
			window.sessionStorage.setItem("lastCountdownValue", targetSeconds);
//			document.getElementById("j_idt12:finishTestButton").click();
		}
	}, 1000);
}

function showInitial(currentDate, targetSeconds) {
	if (targetSeconds > 0) {
		var targetDate = new Date(currentDate); 
		targetDate.setSeconds(currentDate.getSeconds() + targetSeconds);
		var msec = targetDate.getTime() - currentDate.getTime();
		var sec = Math.floor(msec / 1000);
		var mins = Math.floor(sec / 60);
		var hours = Math.floor(mins / 60);
		sec = sec % 60;
		mins = mins % 60;
		hours = hours % 24;
	
		// add leading zeros
		if (sec < 10) {
			sec = "0" + sec;
		} 
		if (mins < 10) {
			mins = "0" + mins;
		}
		if (hours < 10) {
			hours = "0" + hours;
		}
		document.getElementById("countdown").innerHTML = hours + ":" + mins + ":" + sec;
	} else {
		document.getElementById("countdown").innerHTML = "00:00:00";
	}
}